﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;


namespace RegExpressViewModel.Commands
{
	public class ActionCommand : IVMCommand
	{

		public ActionCommand(Action executeAction)
		{
			ExecuteAction = executeAction;
		}


		public ActionCommand(Func<bool> canExecuteCheck, Action executeAction)
		{
			CheckCanExecute = canExecuteCheck;
			ExecuteAction = executeAction;
		}



		/// <summary>
		/// Gets or sets the Action delegate to call when the command is executed.
		/// </summary>
		/// <value>The Action delegate to call when the command is executed.</value>
		public Action ExecuteAction { get; set; }


		/// <summary>
		/// Gets or sets the method to call when checking whether the command may be executed.
		/// </summary>
		/// <value>The delegate to call.</value>
		/// <remarks>If this is not set, the command will be assumed to be executable by default.</remarks>
		public Func<bool> CheckCanExecute { get; set; }



		#region ICommand Members

		public event EventHandler CanExecuteChanged;


		public void RefreshCanExecute()
		{
			if (CanExecuteChanged != null)
				CanExecuteChanged(this, new EventArgs());
		}

	
		public bool CanExecute(object parameter)
		{
			if (CheckCanExecute != null)
				return CheckCanExecute();

			return true;
		}



		public void Execute(object parameter)
		{
			if (ExecuteAction != null)
				ExecuteAction();
		}


		#endregion


	}
}
